package com.gsoe.problem.controller;

import com.gsoe.base.exception.ProblemException;
import com.gsoe.base.model.PageResult;
import com.gsoe.base.model.Result;
import com.gsoe.problem.dto.ExamDTO;
import com.gsoe.problem.dto.SubmitDTO;
import com.gsoe.problem.service.ExamProblemService;
import com.gsoe.problem.service.ExamRegisterUserService;
import com.gsoe.problem.service.ExamService;
import com.gsoe.problem.utils.BloomFilterUtil;
import com.gsoe.problem.vo.ExamProblemVO;
import com.gsoe.problem.vo.ExamVO;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 考试表 前端控制器
 * </p>
 *
 * @author xiaoma
 * @since 2024-10-27
 */
@RestController
@RequestMapping("/problem/exam")
public class ExamController {
    @Autowired
    ExamService examService;

    @Autowired
    ExamRegisterUserService examRegisterUserService;

    @Autowired
    ExamProblemService examProblemService;

    @Autowired
    BloomFilterUtil bloomFilterUtil;

    @PostMapping("/getExamList")
    public Result<PageResult<ExamVO>>getExamList(@RequestBody ExamDTO dto) {
        return Result.success(examService.getExamList(dto));
    }
    @GetMapping("/signUpExam/{examId}")
    public Result<String>signUpExam(@PathVariable("examId") Long examId) {
        if(examId==null)throw new ProblemException(("signUpExam：id不能为null"));
        examRegisterUserService.signUpExam(examId);
        return Result.success("ok");
    }

    @GetMapping("/cancelExam/{examId}")
    public Result<String>cancelExam(@PathVariable("examId") Long examId) {
        if(examId==null)throw new ProblemException(("cancelExam：id不能为null"));
        examRegisterUserService.cancelExam(examId);
        return Result.success("ok");
    }

    @GetMapping("/getExamProblemList/{examId}")
    public Result<ExamProblemVO> getExamProblemList(@PathVariable("examId") Long examId) {
        boolean b = bloomFilterUtil.mightContain(examId.toString());
        if(!b)return Result.fail("未找到该id");
        return examProblemService.getExamProblemList(examId);
    }

    @GetMapping("/getExamRemainingTime/{examId}/{userId}")
    public Result<Long> getExamRemainingTime(@PathVariable(value = "examId") Long examId,@PathVariable(value = "userId") Long userId){
        return Result.success(examService.getExamRemainingTime(examId,userId));
    }

    @PostMapping("/submit")
    public Result<String> submit(@RequestBody @Valid SubmitDTO submitDTO) {
        examRegisterUserService.submit(submitDTO);
        return Result.success("ok");
    }
}
